<template>
  <div class="tree-box">
    <div v-for="item in treeData" :key="item[valueProps]">
      <div class="tree-item" 
          :class="{
            'disabled': item.disabled,
            'selected': item[valueProps] === value,
          }">
       
        <span class="icon-box">
          <span class="arrow" v-if="item.children.length" @click="handleExpand(item)">
            <IconDownOne style="font-size: 16px;" theme="filled" v-if="item.expend"></IconDownOne>
            <IconRightOne style="font-size: 16px;" theme="filled"  v-else></IconRightOne>
          </span>
          <TreeIcon :type="item.type"/>
        </span>
        <span class="label-text" @click="handleSelect(item[valueProps], item.type)">{{ item[labelProps] }}</span>
      </div>
      <Transition appear>
        <div v-if="item.expend">
          <Tree 
            style="padding-left: 20px"
            v-if="item.children.length" 
            :data="item.children" 
            :label-props="labelProps"
            @update:value="(params) => handleSelectChild(params)"
            :value-props="valueProps" :value="value"></Tree>
        </div>
      </Transition>
    </div>
  </div>
</template>

<script lang="ts" setup>
import TreeIcon from './TreeIcon.vue'
import { computed, ref } from 'vue'
interface TreeOption {
  label?: string
  value?: string | number
  disabled?: boolean,
  children: TreeOption[],
  [propN: string]:any //  任意属性
}

const props = withDefaults(defineProps<{
  labelProps: string
  valueProps: string
  value: string | number
  data: TreeOption[]
  disabled?: boolean
}>(), {
  disabled: false,
  labelProps: 'label',
  valueProps: 'value',
})


const emit = defineEmits<{
  (event: 'update:value', payload: any): void
}>()

const treeData = computed(() => {
  props.data.map(item => {
    return item.expend = false
  })
  return props.data
})

const handleExpand = (item: any) => {
  item.expend = !item.expend
}


const handleSelect = (value: string, type: string) => {
  emit('update:value', {value, type})
}

const handleSelectChild = (params:any) => {
  emit('update:value', params)
}

</script>

<style lang="scss" scoped>
.tree-box {
  max-height: 260px;
  /* padding: 5px; */
  overflow: auto;
  text-align: left;
  font-size: 13px;
  user-select: none;
  .tree-item {
    height: 32px;
    line-height: 32px;
    padding: 0 5px 0 15px;
    display: flex;
    align-items: center;
    border-radius: $borderRadius;

    cursor: pointer;
    @include ellipsis-oneline();

    &.disabled {
      color: #b7b7b7;
      cursor: default;
    }
    &:not(.disabled, .selected):hover {
      background-color: rgba($color: #666, $alpha: .05);
    }

    &.selected {
      background-color: rgba($color: $themeColor, $alpha: .05);
    }

    .icon-box{
      display: inline-block;
      height:29px;
      position: relative;
      .arrow{
        position: absolute;
        left: -16px;
      }
    }

    .label-text{
      display: block;
      flex:1;
    }
  }
}


</style> 